import 'dart:async';
import 'package:flame/components.dart';
import 'package:freedom/page/mahjong/common/card_build.dart';
import 'package:freedom/page/mahjong/mahjong_game.dart';
import 'package:freedom/page/mahjong/util/user_location.dart';
import '../common/common_card.dart';
import '../common/mahjong_context.dart';
import 'abstract_component.dart';

/// *
/// 当前用户BOTTOM出牌区域
class RightExportArea extends AbstractExport with HasGameRef<MahjongIndexGame> {
  RightExportArea()
      : super(
          anchor: Anchor.center,
        );

  @override
  FutureOr<void> onLoad() {
    size = MahjongContext().rightExportAreaSize;
    position = MahjongContext().rightExportAreaPoint;
    //debugMode = true;
    angle = radians(270);
    //添加测试出牌数据
    //testAddCard();
    //transform.transformMatrix.setRotationX(radians(30));
    //transform.transformMatrix.setRotationY(radians(270));
    super.onLoad();
  }

  /// 添加卡牌测试数据
  ///
  /// 涉及到翻转删除所以不能直接添加卡牌
  /// ！！！！！！！！！！！！！！！！！！！！！！！！！！
  /// 需要一个临时对象存储

  @override
  void reflashUI(List<int> cards) async {
    eportAllCard.clear();
    removeAll(Set.from(children));
    for (int i = 0, j = cards.length; i < j; i++) {
      var cardInfo = CardBuildUtil.rightExport(getLastPoint(), cards[i]);
      eportAllCard.add(cardInfo..priority = cardIndex--);
    }
    for (var element in eportAllCard) {
      add(element);
    }
    var gameInfo = MahjongContext.roomInfo.gameInfo;
    if (eportAllCard.isNotEmpty &&
        gameInfo.exportUserId == currentUserState.userId &&
        eportAllCard.last.cardCode == gameInfo.exportCard) {
      eportAllCard.last.addMark();
    }
  }

  @override
  Direction userDirection() {
    return Direction.RIGHT;
  }

  Vector2 getLast() {
    // 屏幕的宽度减去 区域定位的中心宽度减去区域的一半加上最后一个元素的位移
    return Vector2(
        MahjongContext().screenWidth -
            position.x -
            size.x / 2 +
            getLastPoint().x,
        MahjongContext().screenHeight -
            position.y +
            size.y / 2 +
            MahjongContext().lrExportCardSize.y +
            getLastPoint().y);
  }

  /**
   * 获取此compont 最后一张牌落地的位置
   */

  Vector2 getLastPoint() {
    //一行向下取整
    var rowSum = (size.x / MahjongContext().lrExportCardSize.x).floor();
    var lastPos = Vector2(
        (size.x - (rowSum - 1) * MahjongContext().lrExportCardSize.x) / 2, 0);
    if (eportAllCard.length > 0) {
      var last = eportAllCard.last;
      lastPos = Vector2(
          last.position.x +
              last.size.x * MahjongContext().lrHandCardImageHBRatio,
          lastPos.y);
      //计算出当前所在行和当前行最大数
      var addRowCount = 0;
      var currentRow = 0;
      while (eportAllCard.length + 1 > addRowCount) {
        currentRow++;
        addRowCount += ((currentRow - 1) * 2 + rowSum);
      }
      lastPos.y = MahjongContext().lrExportCardSize.y * (currentRow - 1);
      //如果当前行最大数减去最后一行的数量就应该换行了
      if (addRowCount - rowSum - (currentRow - 1) * 2 == eportAllCard.length) {
        // 产生换行并且横向个数 +1
        var lastIndex = addRowCount - rowSum * 2 - 4 * currentRow + 6;
        CommonCard upFi = eportAllCard[lastIndex];
        //首个麻将要产生位移
        lastPos.x = upFi.position.x -
            upFi.size.x * MahjongContext().lrHandCardImageHBRatio;
      }
    }
    return lastPos;
  }

  @override
  CommonCard exportCard(int card) {
    var cardInfo = CardBuildUtil.rightExport(getLastPoint(), card)
      ..priority = cardIndex--;
    eportAllCard.add(cardInfo);
    add(cardInfo);
    return cardInfo;
  }

  @override
  Vector2 getLastPointByScc(Vector2 currentCardPoint) {
    //获取出牌相当于屏幕的所在
    Vector2 targetScreenPoint =
        MahjongContext().rightHandCardAreaPoint + currentCardPoint;
    //获取反转过后 相对于屏幕的右上角所在位置
    Vector2 exportPoint = Vector2(
        MahjongContext().rightExportAreaPoint.x -
            MahjongContext().leftExportAreaSize.y / 2,
        MahjongContext().rightExportAreaPoint.y +
            MahjongContext().leftExportAreaSize.x / 2);
    //获取资源位移距离
    Vector2 sourceScreenPoint = Vector2(
        exportPoint.x +
            getLastPoint().y +
            MahjongContext().lrExportCardSize.y / 2,
        exportPoint.y - getLastPoint().x);
    Vector2 range = sourceScreenPoint - targetScreenPoint;
    return range;
  }
}
